/* Copyright (C) 2003, Charles Wang.
 * Author:  Charles Wang <charles@linux.net.cn>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANT; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public LIcense for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include  <errno.h>
#include  <stdio.h>
#include  <stdlib.h>
#include  <string.h>
#include  "newtutils.h"
#include  "mblog.h"

static char gateway[20] = "";
static char dns1[20] = "";
static char dns2[20] = "";

const char * gateway_dns_runner(void) {
    newtComponent  form, ok_btn, cancel_btn;
    newtGrid       grid, btngrid;
    ipinput        gwii, dns1ii, dns2ii;
    FILE         * resolv_fp;
    int            gw_err;
    char           cmdbuf[128];

    grid = newtCreateGrid(1, 4);

    ipinput_create(&gwii,   "Default Gateway", gateway);
    ipinput_create(&dns1ii, "DNS 1          ", dns1);
    ipinput_create(&dns2ii, "DNS 2          ", dns2);

    btngrid = newtCreateGrid(2, 1);
    ok_btn = newtButton(-1, -1, "Take Effect");
    newtGridSetField(btngrid, 0, 0, NEWT_GRID_COMPONENT, ok_btn,
		     0, 0, 0, 0, 0, 0);
    cancel_btn = newtButton(-1, -1, "Cancel");
    newtGridSetField(btngrid, 1, 0, NEWT_GRID_COMPONENT, cancel_btn,
		     0, 0, 0, 0, 0, 0);

    newtGridSetField(grid, 0, 0, NEWT_GRID_SUBGRID, gwii.grid,
		     1, 1, 1, 0, 0, 0);
    newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, dns1ii.grid,
		     1, 0, 1, 0, 0, 0);
    newtGridSetField(grid, 0, 2, NEWT_GRID_SUBGRID, dns2ii.grid,
		     1, 0, 1, 0, 0, 0);
    newtGridSetField(grid, 0, 3, NEWT_GRID_SUBGRID, btngrid,
		     1, 1, 1, 0, 0, 0);

    form = newtExtForm(NULL, NULL, 0);
    newtGridAddComponentsToForm(grid, form, 1);

    newtGridWrappedWindow(grid, "Default Gateway and DNS");
    newtGridFree(grid, 1);

    for (;;) {
	if (newtExtRunForm(form) == cancel_btn)  break;

	gw_err = 0;
	ipinput_get_ip(&gwii, gateway);
	if (strcmp(gateway, "INVALID")) {
	    snprintf(cmdbuf, sizeof(cmdbuf),
		     "/sbin/route add default gw %s", gateway);
	    if (system(cmdbuf)) {
		mblog("Run '%s' failed.\n", cmdbuf);
		newtWinMessage("Error", "OK",
			       "Run route to setup default gateway failed.");
		gw_err = 1;
	    } else
		mblog("Run '%s' successed.\n", cmdbuf);
	}

	resolv_fp = fopen("/etc/resolv.conf", "w");
	if (resolv_fp == NULL)
	    newtWinMessage("Error", "OK",
			   "Can't setup DNS: %s", strerror(errno));
	else {
	    ipinput_get_ip(&dns1ii, dns1);
	    if (strcmp(dns1, "INVALID"))
		fprintf(resolv_fp, "nameserver %s\n", dns1);
	    ipinput_get_ip(&dns2ii, dns2);
	    if (strcmp(dns2, "INVALID"))
		fprintf(resolv_fp, "nameserver %s\n", dns2);
	    fclose(resolv_fp);

	    if (!gw_err) {
		newtWinMessage("SUCCESS", "OK",
			       "Set the default gateway and DNS successfully.");
		break;
	    }
	}
    }

    newtFormDestroy(form);
    newtPopWindow();

    return  NULL;
}

void gateway_dns_saver(FILE *fp) {
    fprintf(fp, "gateway = '%s'\n", gateway);
    fprintf(fp, "dns1 = '%s'\n", dns1);
    fprintf(fp, "dns2 = '%s'\n", dns2);
}
